package net.wanho.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.wanho.common.ProductConstant;
import net.wanho.dto.ProductDTO;
import net.wanho.dto.ProductQueryDTO;
import net.wanho.mapper.ProductMapper;
import net.wanho.po.Product;
import net.wanho.service.ProductService;
import net.wanho.util.CurrentUtil;
import net.wanho.vo.PageInfo;
import net.wanho.vo.ProductExportVo;
import net.wanho.vo.ProductVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author 32033
 * @description 针对表【product(商品表)】的数据库操作Service实现
 * @createDate 2025-01-11 17:01:23
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService {

    @Override
    public PageInfo<ProductVo> pageList(int pageNum, int pageSize, ProductDTO productDTO) {
        Page<ProductVo> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ProductVo> wrapper = new QueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(productDTO.getStoreId()), "store_id", productDTO.getStoreId());
        wrapper.like(!ObjectUtils.isEmpty(productDTO.getProductName()), "brand_name", productDTO.getProductName());
        wrapper.eq(!ObjectUtils.isEmpty(productDTO.getBrandName()), "brand_name", productDTO.getBrandName());
        wrapper.eq(!ObjectUtils.isEmpty(productDTO.getTypeName()), "type_name", productDTO.getTypeName());
        wrapper.eq(!ObjectUtils.isEmpty(productDTO.getSupplyName()), "supply_name", productDTO.getSupplyName());
        wrapper.eq(!ObjectUtils.isEmpty(productDTO.getPlaceName()), "place_name", productDTO.getPlaceName());
        wrapper.eq(!ObjectUtils.isEmpty(productDTO.getUpDownState()), "up_down_state", productDTO.getUpDownState());
//        wrapper.eq(!ObjectUtils.isEmpty(productDTO.getIsOverDate()), "is_over_date", productDTO.getIsOverDate());
        if (productDTO.getIsOverDate() != null && productDTO.getIsOverDate() == 0) {
            wrapper.gt("supp_date", LocalDateTime.now());
        } else if (productDTO.getIsOverDate() != null && productDTO.getIsOverDate() == 1) {
            wrapper.lt("supp_date", LocalDateTime.now());
        }
        this.baseMapper.selectByPage(page, wrapper);

        PageInfo<ProductVo> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page.getCurrent());
        pageInfo.setPageSize(page.getSize());
        pageInfo.setTotalNum(page.getTotal());
        pageInfo.setPageCount(page.getCurrent());
        pageInfo.setResultList(page.getRecords());
        pageInfo.setLimitIndex(page.offset());

        return pageInfo;
    }

    @Override
    public void addProduct(Product product) {
        product.setCreateTime(new Date());
        product.setCreateBy(CurrentUtil.getId());
        product.setUpDownState(ProductConstant.UP_DOWN_STATE_UP);
        this.baseMapper.insert(product);
    }

    @Override
    public List<ProductExportVo> exportTable(ProductQueryDTO product) {
//        Page<Product> page = new Page<>(pageNum, pageSize);
//        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
//        wrapper.like(!ObjectUtils.isEmpty(product.getProductName()), Product::getProductName, product.getProductName());
//        wrapper.like(!ObjectUtils.isEmpty(product.getProductNum()), Product::getProductNum, product.getProductNum());
//        wrapper.like(!ObjectUtils.isEmpty(product.getIntroduce()), Product::getIntroduce, product.getIntroduce());
//        wrapper.like(!ObjectUtils.isEmpty(product.getUpDownState()), Product::getUpDownState, product.getUpDownState());
//        wrapper.like(!ObjectUtils.isEmpty(product.getImgs()), Product::getImgs, product.getImgs());
//        wrapper.like(!ObjectUtils.isEmpty(product.getProductDate()), Product::getProductDate, product.getProductDate());
//        wrapper.like(!ObjectUtils.isEmpty(product.getSuppDate()), Product::getSuppDate, product.getSuppDate());
//        wrapper.like(!ObjectUtils.isEmpty(product.getThreshold()), Product::getThreshold, product.getThreshold());
//        wrapper.like(!ObjectUtils.isEmpty(product.getInPrice()), Product::getInPrice, product.getInPrice());
//        wrapper.like(!ObjectUtils.isEmpty(product.getSalePrice()), Product::getSalePrice, product.getSalePrice());
//        wrapper.like(!ObjectUtils.isEmpty(product.getMemPrice()), Product::getMemPrice, product.getMemPrice());
//        wrapper.like(!ObjectUtils.isEmpty(product.getCreateTime()), Product::getCreateTime, product.getCreateTime());
//        wrapper.like(!ObjectUtils.isEmpty(product.getUpdateTime()), Product::getUpdateTime, product.getUpdateTime());
//        wrapper.like(!ObjectUtils.isEmpty(product.getCreateBy()), Product::getCreateBy, product.getCreateBy());
//        wrapper.like(!ObjectUtils.isEmpty(product.getUpdateBy()), Product::getUpdateBy, product.getUpdateBy());
//        this.page(page, wrapper);
//        return page.getRecords();
        // TODO 封装导出数据 还没太懂
        return baseMapper.selectProductExportList(product);
    }

    @Override
    public void deleteProduct(Integer id) {
        Product product = this.baseMapper.selectById(id);
        if (product.getProductInvent() > 0)
            throw new RuntimeException("该商品还有库存，不能删除");
        this.baseMapper.deleteById(id);
    }
}




